﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineStore.Data_Access_Layer;
using System.Data.Objects;
using System.Drawing;
using System.IO;

namespace OnlineStore.Business_Logic_Layer
{
    class BizProduct
    {
        private OnlineStoreEntities eOnlineStoreEntities;

        public BizProduct()
        {
            eOnlineStoreEntities = new OnlineStoreEntities();           
        }
        
        public int SaveProductItem(int nID, string strProduct_ID, string strProduct_Name, string strType, string strColor, string strSize, string strParent, string strIntroduce, string strProduct_Detail, string strPicture, string strPrice, Boolean IsClocked, Image Pro_Image)
        {
            try
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();                
                tbProduct eProduct = new tbProduct();
                int MaxProductID = 0 ;
                if (nID != -1) // use for update
                    eProduct = GetProductbyKeyWord("ID",nID.ToString()).First();
                else // use for add new
                    MaxProductID = GetMaxProductID();
                                
                
                eProduct.Product_ID = strProduct_ID.ToString();
                eProduct.Product_Name = strProduct_Name.ToString();
                eProduct.Type = strType.ToString();
                eProduct.Color = strColor.ToString();
                eProduct.Size = strSize.ToString();
                eProduct.Introduce = strIntroduce.ToString();
                eProduct.Detail = strProduct_Detail.ToString();
                eProduct.Picture = strPicture.ToString();
                if (strParent != "")                    
                    eProduct.Farther = int.Parse(strParent.ToString());
                eProduct.Price = double.Parse(strPrice.ToString());
                eProduct.Clock = (IsClocked == true ? 1 : 0);
                eProduct.Ord_Qty = 0;
                eProduct.Pur_Qty = 0;
                eProduct.Stock_Qty = 0;

                // Save product picture
                Pro_Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                eProduct.Pro_Image = ms.ToArray();

                if (nID == -1) // use for add new
                {
                    eProduct.ID = MaxProductID + 1;
                    eOnlineStoreEntities.AddTotbProducts(eProduct);
                }
                return eOnlineStoreEntities.SaveChanges();
            }
            catch (Exception ex)
            {
                ex.ToString();
                return -1;
            }
        }
        private int GetMaxProductID()
        {
            try
            {                
                tbProduct eProduct = new tbProduct();
                var ProductID = from P in eOnlineStoreEntities.tbProducts
                                where P.ID == (eOnlineStoreEntities.tbProducts.Max(P1 => P1.ID))
                                select P.ID;

                return ProductID.First();
            }
            catch (Exception ex)
            {
                ex.ToString();
                return 0;
            }
        }
    /*
        // search by product name
        public List<tbProduct> GetProductListbyKeyWord(string strKeyWord)
        {
            List<tbProduct> ProductList = new List<tbProduct>();
            try
            {             
                
                ProductList = eOnlineStoreEntities.tbProducts.Where(i => i.Product_Name.Contains(strKeyWord)).ToList();                
            }
            catch (Exception ex)
            {
                ex.ToString();                
            }
            return ProductList;
        }
        //search by ID
        public tbProduct GetProductbyID(int nID)
        {
            try
            {
                tbProduct nProduct = eOnlineStoreEntities.tbProducts.Where(i => i.ID == nID).First();
                return nProduct;
            }
            catch (Exception ex)
            {
                ex.ToString();
                return null;
            }
        } */

        // Get Product by ID => Get Product by key word
        public List<tbProduct> GetProductbyKeyWord(string strKey, string strValue)
        {
            List<tbProduct> ProductList = new List<tbProduct>();
            try
            {
                if (strKey == "")
                    ProductList = eOnlineStoreEntities.tbProducts.ToList();

                if (strKey == "ID")
                {
                    int nID = int.Parse(strValue);
                    ProductList = eOnlineStoreEntities.tbProducts.Where(i => i.ID == nID).ToList();
                }
                if (strKey == "Product ID")
                    ProductList = eOnlineStoreEntities.tbProducts.Where(i => i.Product_ID == strValue).ToList();
                if (strKey == "Name")
                    ProductList = eOnlineStoreEntities.tbProducts.Where(i => i.Product_Name.Contains(strValue)).ToList();
                
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return ProductList;
        }
        public void UpdatePurchasetoStockProduct(int nProduct_ID, int nPurchaseQty, int nStockQty)
        {
            tbProduct eProduct = new tbProduct();
            eProduct = GetProductbyKeyWord("ID",nProduct_ID.ToString()).First();
            eProduct.Pur_Qty = eProduct.Pur_Qty - nPurchaseQty;
            eProduct.Stock_Qty = eProduct.Stock_Qty + nStockQty;
            eOnlineStoreEntities.SaveChanges();
        }

        public void UpdateOrdertoStockProduct(int nProduct_ID, int nOrderQty, int nExportQty)
        {
            tbProduct eProduct = new tbProduct();
            eProduct = GetProductbyKeyWord("ID", nProduct_ID.ToString()).First();
            eProduct.Ord_Qty = eProduct.Ord_Qty - nOrderQty;
            eProduct.Stock_Qty = eProduct.Stock_Qty - nExportQty;
            eOnlineStoreEntities.SaveChanges();
        }
        // update order qty when user update confirm qty.
        public void UpdateConfirmOrderProduct(int nProduct_ID, int nOrderQty, int nConfirmQty)
        {
            tbProduct eProduct = new tbProduct();
            eProduct = GetProductbyKeyWord("ID", nProduct_ID.ToString()).First();
            eProduct.Ord_Qty = eProduct.Ord_Qty - nOrderQty + nConfirmQty;
            eOnlineStoreEntities.SaveChanges();
        }
        public int DeleteProductItem(int nID)
        {
            try
            {
                tbProduct eProduct = new tbProduct();
                eProduct = GetProductbyKeyWord("ID", nID.ToString()).First();
                eOnlineStoreEntities.tbProducts.DeleteObject(eProduct);
                return eOnlineStoreEntities.SaveChanges();                
            }
            catch(Exception ex)
            {
                ex.ToString();
                return -1;
            }
        }
        public void UpdatePurchaseQty(string strProductID, int nPurchaseQty)
        {
            try
            {
                tbProduct eProduct = new tbProduct();                
                eProduct = GetProductbyKeyWord("ID", strProductID).First();
                eProduct.Pur_Qty = eProduct.Pur_Qty + nPurchaseQty;
                eOnlineStoreEntities.SaveChanges();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
    }
}
